package com.bysc.executor.handler;

import com.bysc.baseservice.config.RabbitmqConfig;
import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.RedisConstant;
import com.bysc.commonutils.vo.JoinForm;
import com.bysc.executor.client.JoinClient;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.bysc.commonutils.constant.RedisConstant.JOIN_FORM;

/**
 * 任务处理器
 */
@Component
public class JoinJobHandler {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private JoinClient joinClient;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 报名表录取结果缓存预热
     */
    @XxlJob("JoinRedisPreheatJob")
    public void JoinRedisPreheatJob() throws Exception {
        try {
            System.out.println("JoinRedisPreheatJob执行了.... " + LocalDateTime.now());
            Results results = joinClient.getAllJoinForm();
            List<JoinForm> resultList = (List<JoinForm>) results.getData().get("list");
            if (resultList != null) {
                int size = resultList.size();
                for (int i = 0; i < size; i++) {
                    JoinForm joinForm = resultList.get(i);
                    String key = JOIN_FORM + joinForm.getUserId();
                    redisTemplate.opsForValue().set(key, joinForm, RedisConstant.KEY_TIMEOUT, TimeUnit.SECONDS);
                    XxlJobHelper.log("data= {}", joinForm); //写日志到xxl-job中
                }
            }
            System.out.println("JoinRedisPreheatJob执行结束了.... " + LocalDateTime.now());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaiYunException(20001, "报名表缓存预热错误");
        }
    }


    /**
     * 每天1点查询活动列表
     */
    @XxlJob("JoinActivitiesInitJob")
    public void JoinActivitiesInitJob() throws Exception {
        try {
            System.out.println("JoinActivitiesInitJob执行了.... " + LocalDateTime.now());
            rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_ACTIVITY, RabbitmqConfig.ROUTINGKEY_ACTIVITY_INIT, "111");
            System.out.println("JoinActivitiesInitJob执行结束了.... " + LocalDateTime.now());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaiYunException(20001, "");
        }
    }
    /**
     * 每天23点查询活动列表
     */
    @XxlJob("JoinActivitiesEndJob")
    public void JoinActivitiesEndJob() throws Exception {
        try {
            System.out.println("JoinActivitiesEndJob执行了.... " + LocalDateTime.now());
            rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_TOPICS_ACTIVITY, RabbitmqConfig.ROUTINGKEY_ACTIVITY_END, "111");
            System.out.println("JoinActivitiesEndJob执行结束了.... " + LocalDateTime.now());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaiYunException(20001, "");
        }
    }
}